<template>
  <div class="page-cont">
    <van-nav-bar
      title="调整任务计划"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <div class="list">
      <task-item 
        v-for="(task, index) in taskList"
        :key="index"
        v-model="taskList[index]"
      />
    </div>
    <van-button
      class="bottom"
      type="info"
      block
      @click="setTaskData"
    >
      调整
    </van-button>
  </div>
</template>

<script>
import TaskItem from './components/TaskItem.vue'
export default {
  name: "DayPlanSetPage",
  components: {
    TaskItem
  },
  data() {
    return {
      plan: null,
      taskList: [],
    };
  },
  created() {
    this.plan = JSON.parse(this.$route.query.json)
    this.getTaskList()
  },
  methods: {
    async getTaskList() {
      let res = await this.api.GET_DAY_PLAN_TASK({searchName: 'dayPlanId', searchKeyword: this.plan.id, pageNo: 1, pageSize: 100000})
      if (res.list.length) {
        let list = res.list.sort((a, b) => a.procedureOrder - b.procedureOrder)
        this.$set(this, 'taskList', list)
      } else {
        let list = this.preData.procedure.map(item => {
          return {
            beamNo: this.plan.beamNo,
            procedureOrder: item.procedureOrder
          }
        })
        this.$set(this, 'taskList', list)
      }
    },
    async setTaskData() {
      let prebeams = this.preData.prebeams.find(item => item.id === this.plan.preBridgeId)
      let tasks = this.taskList.map(item => {
        item.dayPlanId = this.plan.id,
        item.preBridgeId = this.plan.preBridgeId,
        item.beamNo = this.plan.beamNo,
        item.beltlineNo = item.beltlineNo || prebeams.beltlineNo,
        item.status = item.status || 1
        return item
      })
      this.$toast.loading({  message: '加载中...', forbidClick: true });
      await this.api.BATCH_UPDATE_TASK({dayPlanMngs: tasks})
      this.$toast.clear()
      this.$router.back()
    }
  }
};
</script>

<style lang="scss" scoped>
.page-cont {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f5f5;
  .list {
    flex: 1;
    overflow: hidden;
    overflow-y: auto;
  }
}
</style>
